#!/bin/bash

MASTER01=192.168.56.11
MASTER02=192.168.56.12
MASTER03=192.168.56.13
CONTAINERD_VERSION=1.7.9
RUNC_VERSION=1.1.10
CNI_VERSION=1.3.0
PROXY=192.168.0.109:10809

# https://github.com/containerd/containerd/blob/main/docs/getting-started.md
mkdir -p ~/containerd
curl --proxy $PROXY -L -o ~/containerd/containerd-$CONTAINERD_VERSION-linux-amd64.tar.gz https://github.com/containerd/containerd/releases/download/v$CONTAINERD_VERSION/containerd-$CONTAINERD_VERSION-linux-amd64.tar.gz
curl --proxy $PROXY -L -o ~/containerd/containerd.service https://raw.githubusercontent.com/containerd/containerd/main/containerd.service 
curl --proxy $PROXY -L -o ~/containerd/runc.amd64 https://github.com/opencontainers/runc/releases/download/v$RUNC_VERSION/runc.amd64 
curl --proxy $PROXY -L -o ~/containerd/cni-plugins-linux-amd64-v$CNI_VERSION.tgz https://github.com/containernetworking/plugins/releases/download/v$CNI_VERSION/cni-plugins-linux-amd64-v$CNI_VERSION.tgz 

ssh vagrant@$MASTER02 "mkdir -p ~/containerd"
ssh vagrant@$MASTER03 "mkdir -p ~/containerd"
scp ~/containerd/* vagrant@$MASTER02:~/containerd
scp ~/containerd/* vagrant@$MASTER03:~/containerd


mkdir -p ~/containerd
sudo cp ~/containerd/containerd.service /usr/lib/systemd/system/containerd.service

## install containerd
sudo tar Cxzvf /usr/local ~/containerd/containerd-$CONTAINERD_VERSION-linux-amd64.tar.gz
sudo systemctl daemon-reload
sudo systemctl enable --now containerd

## Installing runc(命令行工具)
sudo install -m 755 ~/containerd/runc.amd64 /usr/local/sbin/runc

## Installing CNI plugins(Container Networking Interface)
sudo mkdir -p /opt/cni/bin
sudo tar Cxzvf /opt/cni/bin ~/containerd/cni-plugins-linux-amd64-v$CNI_VERSION.tgz

##设置containerd
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
# 设置cgroup驱动为systemd
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
# 替换沙箱（pause）镜像地址
sudo sed -i 's#sandbox_image = ".*"#sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"#' /etc/containerd/config.toml

# 更换国内镜像 
# 修改 /etc/containerd/config.toml 中的
sudo vi /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".registry]
    config_path = "/etc/containerd/certs.d"  # 镜像地址配置文件

# 配置docker.io的镜像地址
sudo mkdir -p /etc/containerd/certs.d/docker.io
cat <<EOF | sudo tee /etc/containerd/certs.d/docker.io/hosts.toml
server = "https://docker.io"
[host."https://docker.mirrors.ustc.edu.cn"]
  capabilities = ["pull", "resolve", "push"]
EOF

# 设置Proxy 有梯子的可以配置这个
# sudo mkdir -p /etc/systemd/system/containerd.service.d
# cat <<EOF | sudo tee /etc/systemd/system/containerd.service.d/http-proxy.conf
# [Service]
# Environment="HTTP_PROXY=http://192.168.0.109:10809"
# Environment="HTTPS_PROXY=http://192.168.0.109:10809"
# Environment="NO_PROXY=localhost,127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,.svc,.cluster.local,.ewhisper.cn,<nodeCIDR>,<APIServerInternalURL>,<serviceNetworkCIDRs>,<etcdDiscoveryDomain>,<clusterNetworkCIDRs>,<platformSpecific>,<REST_OF_CUSTOM_EXCEPTIONS>"
# EOF

sudo systemctl daemon-reload
sudo systemctl restart containerd

# 测试 需要root权限，如果非root安装，要切到root去测试： su  退出 exit
ctr image pull docker.io/library/nginx:alpine
ctr run -d docker.io/library/nginx:alpine nginx
ctr container ls
ctr images ls
ctr image rm nginx
ctr task kill nginx
ctr container rm nginx
ctr container info nginx


